﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using OTS.Model;
using OTS.BLL;

public partial class member_Collection : OTS.MemberPage
{
    protected string strConent;
    FavoriteBLL FavBll = new FavoriteBLL();
    Product product = new Product();
    ProductBLL productBll = new ProductBLL();
    ProductSizeBLL prosizeBLL = new ProductSizeBLL();
    color col = new color();
    ColorBLL colorBLL = new ColorBLL();
    custom cus = new custom();
    DataTable dt = new DataTable();
    DataTable dTable = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!TestLogin(HttpContext.Current))
        {
            Response.Redirect("index.aspx");
            return;
        }
        if (!Page.IsPostBack)
        {
            FillCollection();
        }
    }
    private void FillCollection()
    {
        cus = GetCustom(HttpContext.Current);
        dt = FavBll.GetListByCusID(cus.cusID);
        StringBuilder str = new StringBuilder();

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            str.Append("<tr favID=\"" + dt.Rows[i]["favID"].ToString() + "\">");
            //str.Append("<td style=\"vertical-align: middle; text-align: center; width: 20px;\">");
            //str.Append("<input type='checkbox' />");
            //str.Append("</td>");
            str.Append("<td class=\"goodpic\" width=\"120\">");
            str.AppendFormat("<a title=\"" + dt.Rows[i]["title"].ToString() + dt.Rows[i]["proNO"].ToString() + "\" href=\"product.aspx?proID=" + dt.Rows[i]["proID"].ToString() + "\" style=\"display: block; width: 120px; height: 160px;\">");
            if (dt.Rows[i]["bigImg_pic"].ToString()=="")
            {
                str.AppendFormat("<img width='120' height='160' alt=\"" + dt.Rows[i]["title"].ToString() + dt.Rows[i]["proNO"].ToString() + "\" src=\"img/yes_picture.gif\" />");
            }
            else
            {
                str.AppendFormat("<img width='120' height='160' alt=\"" + dt.Rows[i]["title"].ToString() + dt.Rows[i]["proNO"].ToString() + "\" src=\"" + PictureProcessing.ImgPathToThumbURL(dt.Rows[i]["bigImg_pic"].ToString()) + "\" />");
            }
            str.Append("</a></td>");
            str.Append("<td class=\"goodinfo\" width=\"60%\"> <h6>");
            str.AppendFormat("<a  style=\"border:0;\" title=\"" + dt.Rows[i]["title"].ToString() + dt.Rows[i]["proNO"].ToString() + "\" href=\"product.aspx?proID=" + dt.Rows[i]["proID"].ToString() + "\">" + dt.Rows[i]["title"].ToString() + dt.Rows[i]["proNO"].ToString() + "</a>");
            str.Append("<h6> </td>");
            str.Append("<td class=\"price_button\" width=\"7%\"> <ul>");
            str.AppendFormat("<li><span class=\"price1\">￥" + dt.Rows[i]["retailPrice"].ToString() + "</span></li>");
            str.Append("</ul> </td>");
            str.Append("<td><ul class=\"button\">");
            str.Append("<li class=\"addcart buy-select\" id=\"update\" value=\"" + dt.Rows[i]["proID"].ToString() + "\" style=\"z-index: 10;\">");
            str.Append("<a class=\"lnk\" style=\"border:0;\">加入购物车</a>");
            str.Append("<div id=\"buy-select-list" + dt.Rows[i]["proID"].ToString() + "\" class=\"buy-select-list\" style=\"display: none;margin-left:-200px;margin-top:7px;\">");
            str.Append("<h3>请选择颜色和尺码</h3>");
            str.Append("<table width=\"100%\"><tbody><tr>");
            str.Append("<td style=\"clear: right; overflow: hidden; width: 10%; white-space: nowrap; vertical-align: middle;\">颜色：</td>");
            str.Append("<td colspan=\"2\"><ul class=\"ulColor\">");

            product = productBll.GetSingleProduct(Convert.ToInt32(dt.Rows[i]["proID"].ToString()));
            string[] strCol = product.color.Split(',');
            for (int j = 0; j < strCol.Length; j++)
            {
                col = colorBLL.GetColorByID(strCol[j]);
                str.Append("<li value=\"" + col.colID + "\">");
                str.AppendFormat("<a href=\"#\" style=\"text-indent:0;\"><span>" + col.col_color + "</span><i></i></a>");
                str.Append("</li>");
            }

            str.Append("</ul></td></tr>");
            str.Append("<tr>");
            str.Append("<td style=\"width: 100; white-space: nowrap; padding-right: 0px;\">尺码：</td>");
            str.Append("<td style=\"line-height: 22px;\" colspan=\"2\"><ul class=\"ulSize\">");

            string[] size = product.sizeIDs.Split(',');
            for (int k = 0; k < size.Length; k++)
            {
                dTable = prosizeBLL.GetBySizeID(Convert.ToInt32(size[k]));
                str.Append("<li value=\"" + dTable.Rows[0]["sizeID"].ToString() + "\">");
                str.AppendFormat("<a href=\"#\" style=\"text-indent:0;\"><span>" + dTable.Rows[0]["sizeName"].ToString() + "</span><i></i></a>");
                str.Append("</li>");
            }

            str.Append("</ul></td></tr>");
            str.Append("<tr>");
            str.Append("<td>购买数量：</td>");
            str.Append("<td width=\"20px;\"><div class=\"Numinput\">");
            str.Append("<input id=\"txtCount\" class=\"_x_ipt textcenter\" type=\"text\" value=\"1\" size=\"3\">");
            str.Append("<span class=\"numadjust increase\" onclick=\"ItemNumAdd(this,1)\"></span><span class=\"numadjust decrease\" onclick=\"ItemNumAdd(this,-1);\"></span>");
            str.Append("</div></td>");
            str.Append("<td><span>&nbsp;(库存<span class='store'>0</span>)</span></td>");
            str.Append("</tr>");
            str.Append("<tr>");
            str.Append("<td colspan=\"6\" align=\"right\">");
            str.Append("<input type=\"button\" id=\"btnAdd\" value=\"确定\" onclick=\"DoAdd()\">");
            str.Append("</td>");
            str.Append("</tr></tbody></table>");
            str.Append("</div>");
            str.Append("</li>");
            str.Append("</ul>");
            str.Append("</td>");
            str.Append("<td><ul class=\"button\">");
            str.Append("<li class=\"star-del\"><a href=\"javascript:DeleteCollection(" + dt.Rows[i]["favID"].ToString() + ",this);\" class=\"listact\" style=\"border:0;height:25px;\">删除收藏</a> </li>");
            str.Append("</ul></td>");
            str.Append("</tr>");
            str.Append("<tr><td colspan=\"6\">");
            str.Append("<div style=\"border-bottom: 1px solid #DDD; height: 20px; margin-top: 10px; margin-bottom: 10px;\"></div>");
            str.Append("</td></tr>");
        }
        strConent = str.ToString();
    }
    
    //增加到购物车
    protected void btnAddFav_Click(object sender, EventArgs e)
    {
        Cart cart = new Cart();
        CartBLL cartBll = new CartBLL();

        cus = GetCustom(HttpContext.Current);
        cart.cusID = cus.cusID;
        if (this.txtProID.Text!="" && this.txtCount.Text != "" && this.txtColor.Text != "" && this.txtSize.Text != "")
        {
            cart.proID = Convert.ToInt32(this.txtProID.Text);
            cart.count = Convert.ToInt32(this.txtCount.Text);
            cart.colID = Convert.ToInt32(this.txtColor.Text);
            cart.sizeID = Convert.ToInt32(this.txtSize.Text);
            DataTable dt = cartBll.GetByCart("where cusID=" + cus.cusID + " and proID=" + Int32.Parse(this.txtProID.Text));
            if (dt.Rows.Count > 0)
            {
                JavascriptAlert("此商品已在您的购物车中！");
                FillCollection();
                this.upFav.Update();
                return;
            }
            else
            {
                if (cartBll.AddCart(cart))
                {
                    JavascriptAlert("添加到购物车成功！");
                    FillCollection();
                    this.upFav.Update();
                }
                else
                {
                    JavascriptAlert("对不起，添加到购物车失败！");
                    FillCollection();
                    this.upFav.Update();
                }
            }
        }
        else
        {
            JavascriptAlert("您还没有进行相关选择！");
            FillCollection();
            this.upFav.Update();
            return;
        }
    }

    private void JavascriptAlert(string message)
    {
        ScriptManager.RegisterClientScriptBlock(this.UpdatePanelAlert, typeof(UpdatePanel), "alert1", "alert('" + message + "')", true);
        this.UpdatePanelAlert.Update();
    }
}
